/*
 * Copyright 2010 Joshua Smith and Nelson Llewellyn
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package norse.client;

import norse.model.NorseJfxScan;
import java.io.File;
import java.io.FileInputStream;
import javafx.data.pull.PullParser;
import javafx.data.pull.Event;
import norse.model.NorseJfxHost;
import norse.model.NorseJfxIpv4Address;
import norse.model.NorseJfxMacAddress;
import norse.model.NorseJfxPort;

/**
 * File client for getting Norse data from a file.
 */
public class NorseFileClient extends NorseClient {

        public override function scan() : NorseJfxScan {
                var file = new File(
        "/Users/josh/Dropbox/Projects/Norse/NoRSe_Common/data/NmapScanResults.xml");
        var input = new FileInputStream(file);
        
        var model = NorseJfxScan{};
        var host:NorseJfxHost;
        var port:NorseJfxPort;
        // TODO populate the start and stop times for the scan
        // TODO populate the raw scan data?
        // TODO replace all the printlns with logging at the debug level
        def parser = PullParser {
                    documentType: PullParser.XML;
                    input: input;
                    onEvent: function (event: Event) {
                        if (event.type == PullParser.START_ELEMENT) {
                            if (event.qname.name == "host") {
                                println("Found a new host...");
                                host = NorseJfxHost{};
                            } else if (event.qname.name == "address") {
                                if (event.getAttributeValue("addrtype") == "ipv4") {
                                    var address = event.getAttributeValue("addr");
                                    println("Found ipv4 address: {address}");
                                    insert NorseJfxIpv4Address{
                                            address: address
                                            } into host.addresses;
                                } else if (event.getAttributeValue("addrtype") == "mac") {
                                    var address = event.getAttributeValue("addr");
                                    println("Found mac address: {address}");
                                    var vendor = event.getAttributeValue("vendor");
                                    insert NorseJfxMacAddress{
                                            address: address
                                            vendor: vendor
                                            } into host.addresses;
                                }
                            } else if (event.qname.name == "status") {
                                    var state = event.getAttributeValue("state") == "up";
                                    println("Host up: {state}");
                                    host.up = state;
                            } else if (event.qname.name == "hostname") {
                                    var name = event.getAttributeValue("name");
                                    println("Host is named: {name}");
                                    insert name into host.hostnames;
                            } else if (event.qname.name == "port") {
                                    var protocol = event.getAttributeValue("protocol");
                                    var portId = event.getAttributeValue("portid");
                                    port = NorseJfxPort{
                                            portId: java.lang.Integer.parseInt(portId)
                                            portProtocol: protocol
                                            };
                            } else if (event.qname.name == "state") {
                                    var state = event.getAttributeValue("state");
                                    port.open = state == "open";
                            } else if (event.qname.name == "service") {
                                    var service = event.getAttributeValue("name");
                                    port.service = service;
                            }
                        } else if (event.type == PullParser.END_ELEMENT) {
                                if (event.qname.name == "host") {
                                    println("Finished making new host.");
                                    insert host into model.hosts;
                                } else if (event.qname.name == "port") {
                                    println("Finished making new port.");
                                    insert port into host.ports;
                                }

                        } else if (event.type == PullParser.END_DOCUMENT) {
                                // TODO
                        } 
                    }
                }
        parser.parse();
        println(model);
        return model;
        }

}
